package com.agua.enturno.vista.paginas.prestador;

import com.agua.enturno.vista.BasePage;
import com.agua.enturno.vista.eventos.CambioDeSemanaEvent;
import com.agua.enturno.vista.modelos.CurrentWeekLabelModel;
import com.agua.enturno.vista.modelos.LocalDateModel;
import com.agua.enturno.vista.util.SemanasIterator;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.joda.time.LocalDate;

/**
 * @author Victor Del Rio
 *
 */
public class AgendaNavigationPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	private LocalDate fechaSeleccionada;
	private TextField<String> fechaEspecificaText;
	
	public AgendaNavigationPanel(String id, final SemanasIterator semanasIterator) {
		super(id);
		setOutputMarkupId(true);
		
		add(new AjaxLink<String>("semana-actual-link") {
			private static final long serialVersionUID = 1L;
			@Override
			public void onClick(AjaxRequestTarget target) {
				semanasIterator.reset();
				target.add(AgendaNavigationPanel.this);
				send(getPage(), Broadcast.BREADTH, new CambioDeSemanaEvent(target));
			}
		});
		
		add(new AjaxLink<String>("semana-anterior-link") {
			private static final long serialVersionUID = 1L;
			@Override
			public void onClick(AjaxRequestTarget target) {
				
				if (semanasIterator.hayAnterior()) {
					semanasIterator.anterior();
					target.add(AgendaNavigationPanel.this);
					send(getPage(), Broadcast.BREADTH, new CambioDeSemanaEvent(target));
				}
			}
		});
		
		add(new AjaxLink<String>("semana-siguiente-link") {
			private static final long serialVersionUID = 1L;
			@Override
			public void onClick(AjaxRequestTarget target) {
				
				if (semanasIterator.haySiguiente()) {
					semanasIterator.siguiente();
					target.add(AgendaNavigationPanel.this);
					send(getPage(), Broadcast.BREADTH, new CambioDeSemanaEvent(target));
				}
			}
		});
		
		add(new Label("semana-seleccionada-label", new CurrentWeekLabelModel(new PropertyModel<LocalDate>(semanasIterator, "semanaActual.lunes.fecha"))));
		
		@SuppressWarnings("rawtypes")
		Form form = new Form("feha-especifica-form");
                
		fechaEspecificaText = new TextField<String>("feha-especifica-text", new LocalDateModel(new PropertyModel<LocalDate>(this, "fechaSeleccionada")));
		fechaEspecificaText.setOutputMarkupId(true);
		form.add(fechaEspecificaText);
    	form.add(new AjaxButton("feha-especifica-submit") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {

				try {
						
					semanasIterator.setFecha(fechaSeleccionada);
					send(getPage(), Broadcast.BREADTH, new CambioDeSemanaEvent(target));
					
				} catch (Exception e) {
					target.appendJavaScript("$('#" + fechaEspecificaText.getMarkupId() + "').effect('highlight', {color: '#FFE599'}, 2500);");
				}
				target.add(AgendaNavigationPanel.this);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(((BasePage)this.getPage()).getFeedbackPanel());
			}
		});
    	add(form);
	}
	
}
